package apps.MyJMRI;

import java.awt.*;

import javax.swing.ImageIcon;

import wiiremotej.WiiRemoteJ;
import wiiremotej.WiiRemote;
import wiiremotej.event.WiiRemoteDiscoveredEvent;
import wiiremotej.event.WiiRemoteDiscoveryListener;

import jmri.util.JmriJFrame;

import  net.iharder.dnd.*;

public class RemoteFrame extends JmriJFrame implements
		WiiRemoteDiscoveryListener {
	static org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(RemoteFrame.class.getName());
	private static final long serialVersionUID = 1;
	WiiControl m_control;
//	FileDrop m_fileDrop;

	public RemoteFrame(String name) {
		super(name);
		getContentPane().setLayout(new FlowLayout());
		WiiRemoteJ.findRemotes(this);
		this.setSize(1000, 600);
		m_control = new WiiControl(this);
	}

	public void paint(Graphics g) {
		super.paint(g);

		if (m_control != null) {
//			FileDrop.remove(this);
			setTitle(m_control.getIDString());
			Image img = m_control.getImage();
			setSize(img.getWidth(null), img.getHeight(null));
//			m_fileDrop = new FileDrop(this, m_control);
			g.drawImage(img, 0, 0, this);
		}
	}

	public void wiiRemoteDiscovered(WiiRemoteDiscoveredEvent evt) {
		WiiRemote found;

		found = evt.getWiiRemote();
		try {
			if (m_control.isConnected() == false) {
				m_control = new WiiControl(this);
				found.addWiiRemoteListener(m_control);
				m_control.setRemote(found);
			}
		} catch (Exception e) {
		}
		WiiRemoteJ.stopFind(); // Only 1 at a time
	}

	public void findFinished(int numberFound) {
		log.debug("Found " + numberFound + " remotes!");
	}

}
